package com.zzw.config;

import org.apache.shiro.session.Session;
import org.apache.shiro.session.mgt.SimpleSession;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.SerializationException;
import org.springframework.util.SerializationUtils;

/**
 * 自定义Shiro会话序列化器
 * 使用Java原生序列化替代JSON序列化，避免Jackson反序列化SimpleSession的问题
 */
public class ShiroSessionSerializer implements RedisSerializer<Session> {

    @Override
    public byte[] serialize(Session session) throws SerializationException {
        if (session == null) {
            return new byte[0];
        }
        try {
            return SerializationUtils.serialize(session);
        } catch (Exception e) {
            throw new SerializationException("无法序列化Session对象", e);
        }
    }

    @Override
    public Session deserialize(byte[] bytes) throws SerializationException {
        if (bytes == null || bytes.length == 0) {
            return null;
        }
        try {
            return (Session) SerializationUtils.deserialize(bytes);
        } catch (Exception e) {
            throw new SerializationException("无法反序列化为Session对象", e);
        }
    }
} 